package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.entity.FindPicResult;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 秘境降妖任务
 */
@Slf4j
public class SecretAreaTaskImpl extends AbstractTask {

    private final int overLevel;

    private final String hwndName;

    public SecretAreaTaskImpl(WinDef.HWND hwnd_0, int overLevel, String hwndName) {
        super(hwnd_0);
        this.overLevel = overLevel;
        this.hwndName = hwndName;
    }

    @Override
    public String getTaskName() throws Exception {
        return "秘境降妖";
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc1();
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 2).isFinded()) {
            return true;
        }
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        // 找到“日常活动”并打开
        if (!findAndClickActivity()) {
            return false;
        }

        // 点击参加秘境任务 todo 双倍活跃时候的截图也需要
        Boolean canYuRes = findAndClickTaskTitle(PicConstants.秘境降妖_秘境图标标题, PicConstants.秘境降妖_已完成);
        if (canYuRes == null || !canYuRes) {
            return canYuRes;
        }
        ThreadUtil.sleep3();
        // 点击接受任务
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.秘境降妖_接受任务, 3,0.8)) {
            log.error("【{}】点击“接受任务”失败！", getTaskName());
            return false;
        }
        ThreadUtil.sleep3();

        // 如果是本周第一次进入，需要选择去哪个秘境，默认第一个
        if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.秘境降妖_首次进入, 3)) {
            ThreadUtil.sleep1();
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.秘境降妖_确认进入, 3)) {
                log.error("【{}】首次进入失败！", getTaskName());
                return false;
            }
        }

        // 点击继续挑战
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, PicConstants.秘境降妖_继续挑战, 5)) {
            log.error("【{}】点击“继续挑战”失败！", getTaskName());
            return false;
        }
        ThreadUtil.sleep2();
        // 开始战斗
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, PicConstants.秘境降妖_战斗, 5)) {
            log.error("【{}】点击“开始战斗”失败！", getTaskName());
            return false;
        }
        ThreadUtil.sleep3();

        // 循环战斗检测
        for (int i = 0; i < 50; i++) {
            log.info("【{}】检查完成情况,第[{}]次", getTaskName(), i);

            // 战斗时间
            findBattle();
            // 截止关卡 直接离开
            if (overLevel == 20) {
                if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_截止关卡_20A, 1).isFinded()) {
                    clickEsc1();
                    FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.秘境降妖_离开, 2);
                    log.info("【{}】截止关卡20 离开！", getTaskName());
                    return true;
                }
            } else if (overLevel == 25) {
                if (FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.秘境降妖_截止关卡_25A, PicConstants.秘境降妖_截止关卡_25B}, 1).isFinded()) {
                    clickEsc1();
                    FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.秘境降妖_离开, 2);
                    log.info("【{}】截止关卡25 离开！", getTaskName());
                    return true;
                }
            }
            // 挑战失败 离开
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_失败, 1).isFinded()) {
                clickEsc1();
                FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.秘境降妖_离开, 2);
                log.info("【{}】挑战失败 离开！", getTaskName());
                return true;
            }
            // 继续战斗
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.秘境降妖_继续战斗, 1)) {
                log.info("【{}】点击“继续战斗”", getTaskName());
                continue;
            }
            // 默认点击右侧任务继续战斗
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, PicConstants.秘境降妖_战斗, 3);
        }
        return true;
    }

    @Override
    public Boolean after() throws Exception {
        clickUse(6);
        return true;
    }

}
